package com.exp3;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.BlockingDeque;

import javax.swing.JOptionPane;

public class ReadFromSocket extends Thread {
	private BlockingDeque<String> list;

	public ReadFromSocket(BlockingDeque<String> list) {
		this.list = list;
	}

	@Override
	public void run() {
		Socket socket = null;
		Scanner sc = null;
		try {
			socket = new Socket(InetAddress.getByName("10.5.26.172"), 9999);
			String line = null;
			sc = new Scanner(socket.getInputStream());
			sc.nextLine();
			JOptionPane.showMessageDialog(null, "连接服务器成功");
			while (sc.hasNext()) {
				Thread.sleep(1l);
				line = sc.nextLine();
				list.put(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			sc.close();
		}
	}
}
